# 老师的
# class Pagination:
#
#     def __init__(self, page, data_count, per_num=10, max_show=11):
#         try:
#             page = int(page)
#             if page <= 0:
#                 page = 1
#         except Exception:
#             page = 1
#         self.page = page
#         self.per_num = per_num
#         self.data_count = data_count
#         self.max_show = max_show
#
#         #  1  0   10
#         #  2  10  20
#
#
#
#     @property
#     def start(self):
#         return  (self.page - 1) * self.per_num
#
#     @property
#     def end(self):
#         return  self.page  * self.per_num
#
#     @property
#     def page_html(self):
#
#         # 总页码数
#         sum_page_num, more = divmod(self.data_count, self.per_num)
#         if more:
#             sum_page_num += 1
#         # 最多显示的页码数
#         # max_show = 11
#         half_show = self.max_show // 2
#
#         if sum_page_num <= self.max_show:
#             page_start = 1
#             page_end = sum_page_num
#         else:
#             if self.page - half_show <= 0:
#                 page_start = 1
#                 page_end = self.max_show
#             elif self.page + half_show > sum_page_num:
#                 page_end = sum_page_num
#                 page_start = page_end - self.max_show + 1
#
#             else:
#                 # 页码的起始值
#                 page_start = self.page - half_show
#                 # 页码的终止值
#                 page_end = self.page + half_show
#
#         page_html_list = []
#
#         if self.page == 1:
#
#             page_html_list.append(
#                 '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>')
#         else:
#             page_html_list.append(
#                 '<li><a href="?page={}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'.format(
#                     self.page - 1))
#
#         for i in range(page_start, page_end + 1):
#             if i == self.page:
#                 page_html_list.append('<li class="active"><a href="?page={}">{}</a></li>'.format(i, i))
#             else:
#                 page_html_list.append('<li><a href="?page={}">{}</a></li>'.format(i, i))
#         if self.page == sum_page_num:
#
#             page_html_list.append(
#                 '<li class="disabled"><a aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>')
#         else:
#             page_html_list.append(
#                 '<li><a href="?page={}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(
#                     self.page + 1))
#
#         return ''.join(page_html_list)

#我的
from django.utils.safestring import mark_safe
class Pagination:
    def __init__(self,page,date,pram,num=20,nums=11):
        try:
            page = int(page)  # 获取传过来的页数，获取不到默认为1
        except Exception:
            page = 1
        if page <= 0:
            page = 1

        self.page=page
        self.date=date
        self.num=num
        self.nums=nums
        self.pram=pram
    @property
    def start(self):
        return (self.page - 1) * self.num

    @property
    def end(self):
        return self.page*self.num
    @property
    def page_html(self):
        amount, mod = divmod(self.date, self.num)  # 计算分页数量，有余数加1
        if mod:
            amount += 1
        page_start = self.page - (self.nums - 1) // 2  # 页数显示的起始数
        page_end = self.page + (self.nums - 1) // 2  # 页数显示的末尾数
        # 进行判断前面是否有足够的数据
        if page_start <= 0:
            page_start = 1
            page_end = amount
        # 进行判断后面是否有足够的数据
        elif page_end >= self.date:
            page_start = self.page - self.nums
            page_end = self.date
        page_html_list = []
        page_html_list.append(
            '<li><a href="?page=1" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>')
        if self.page == 1:

            page_html_list.append(
                '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>')
        else:
            self.pram['page']= self.page - 1
            page_html_list.append(
                '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'.format(
                    self.pram.urlencode()))

        for i in range(page_start, page_end + 1):

            self.pram['page'] = i
            if i == self.page:
                page_html_list.append('<li class="active"><a href="?{}">{}</a></li>'.format(self.pram.urlencode(), i))
            else:
                page_html_list.append('<li><a href="?{}">{}</a></li>'.format(self.pram.urlencode(), i))
        if self.page == amount:

            page_html_list.append(
                '<li class="disabled"><a aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>')
        else:
            self.pram['page'] = self.page +1
            page_html_list.append(
                '<li><a href="?{}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(
                    self.pram.urlencode()))
        self.pram['page'] = amount
        page_html_list.append(
            '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>'.format(self.pram.urlencode()))

        return mark_safe(''.join(page_html_list))


'''
在后端
users = [{'name': 'alex-{}'.format(i), 'pwd': 'alexdsb{}'.format(i)} for i in range(1, 355)] 数据

from utils.pagination import Pagination  导入分页器

def user_list(request):
    page_obj = Pagination(request.GET.get('page',1),len(users)) 传入获取到的页数和数据的长度

    return render(request, 'user_list.html', {'users': users[page_obj.start:page_obj.end],'page_html':page_obj.page_html})

'''

